/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.web;

import cz.muni.dto.ShipmentDTO;
import cz.muni.service.CourierServiceLocal;
import cz.muni.service.CustomerServiceLocal;
import cz.muni.service.ShipmentService;
import cz.muni.service.ShipmentServiceLocal;
import cz.muni.service.TarrifServiceLocal;
import java.util.List;
import javax.ejb.EJB;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.Before;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.DontValidate;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.HandlesEvent;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;

/**
 *
 * @author Thomas
 */
@UrlBinding("/Courier-Shipment.{$event}")
public class CourierShipmentActionBean implements ActionBean {

    @EJB(mappedName="java:global/DeliveryCompany-web/ShipmentService!cz.muni.service.ShipmentServiceLocal")
    private ShipmentServiceLocal shipmentService;
    @EJB(mappedName="java:global/DeliveryCompany-web/CustomerService!cz.muni.service.CustomerServiceLocal")
    private CustomerServiceLocal customerService;
    @EJB(mappedName="java:global/DeliveryCompany-web/CourierService!cz.muni.service.CourierServiceLocal")
    private CourierServiceLocal courierService;
    @EJB(mappedName="java:global/DeliveryCompany-web/TarrifService!cz.muni.service.TarrifServiceLocal")
    private TarrifServiceLocal tarrifService;
    private ActionBeanContext context;
    private List<ShipmentDTO> shipments;
    
    @ValidateNestedProperties({
        @Validate(field="sender", required=true),
        @Validate(field="recipient", required=true),
        @Validate(field="courier", required=true),
        @Validate(field="tarrif", required=true),
        @Validate(field="shipmentState", required=true),
        @Validate(field="weight", required=true),
        @Validate(field="distance", required=true),
    })
    private ShipmentDTO shipment;
    
    private Long senderID;
    private Long recipientID;
    private Long courierID;
    private Long tarrifID;

    public Long getSenderID() {
        return senderID;
    }

    public void setSenderID(Long senderID) {
        this.senderID = senderID;
    }

    public Long getRecipientID() {
        return recipientID;
    }

    public void setRecipientID(Long recipientID) {
        this.recipientID = recipientID;
    }

    public Long getCourierID() {
        return courierID;
    }

    public void setCourierID(Long courierID) {
        this.courierID = courierID;
    }

    public Long getTarrifID() {
        return tarrifID;
    }

    public void setTarrifID(Long tarrifID) {
        this.tarrifID = tarrifID;
    }

    public List<ShipmentDTO> getShipments() {
        return shipmentService.findAll();
    }

    public ShipmentDTO getShipment() {
        return shipment;
    }

    public void setShipment(ShipmentDTO shipment) {
        this.shipment = shipment;
    }
    
    @Override
    public void setContext(ActionBeanContext abc) {
        this.context = abc;
    }

    @Override
    public ActionBeanContext getContext() {
        return this.context;
    }
    
    @DefaultHandler
    public Resolution df() {
        return new ForwardResolution("/courier_shipments.jsp");
    }
    
    
    @DontValidate
    @Before(stages = LifecycleStage.BindingAndValidation, on = {"edit", "update"})
    public void loadShipmentFromDatabase() {
        String ids = context.getRequest().getParameter("shipment.id");
        if (ids == null) return;
        shipment = shipmentService.find(Long.parseLong(ids));
    }
 
    @DontValidate
    public Resolution edit() {
        return new ForwardResolution("/editCourierShipment.jsp");
    }
 
    @DontValidate
    public Resolution update() {
        shipmentService.update(shipment);
        return new RedirectResolution("/courier_shipments.jsp");
    }
    
    @DontValidate
    public Resolution delete() {
        shipmentService.delete(shipment);
        return new RedirectResolution("/courier_shipments.jsp");
    }
}
